package com.lancer.anglsrv.tran;

import java.util.Scanner;
import java.util.HashMap;
import java.util.Map;
import java.io.*;

class IndexEntry {
  public String key;
  public int offset;
  public int size;
}

public class Tran {
  Map<String, String> aWords = new HashMap<String, String>();
  Map<String, IndexEntry> aIndex = new HashMap<String, IndexEntry>();  
 
  public Tran() {
  }
  
  //Загрузка списка слов
  private boolean loadWords(String fileNameWords) throws IOException {
    Scanner scn = null;
    String key;
    try {      
      scn = new Scanner(new BufferedReader(new FileReader(fileNameWords)));
      while (scn.hasNext()) {
        key = scn.next();
        aWords.put(key, "");
      }
    }
    finally {
      if (scn != null)
          scn.close();
    }
    return true;
  }
  
  //Загрузка файла индекса для словаря
  private boolean loadIndex(String fileNameIndex) throws IOException {
    BufferedInputStream inputStreamIndex = new BufferedInputStream(new FileInputStream(fileNameIndex));
    String key;
    IndexEntry ind;
    int offset, size, d;
    byte[] b;
    StringBuilder sb = new StringBuilder();
    try {
      int c;
      do {
        
        sb.delete(0, sb.length());
        do {
          c = inputStreamIndex.read();
          if ( (c != -1) && (c != 0 ) ) sb.append((char)c);
        } while ( (c != - 1) && (c != 0) );        
              
        key = sb.toString();
        offset = 0;          
        size = 0;          
       
        if (c == 0) {
          b = new byte[4];
          c = inputStreamIndex.read(b, 0, 4);
          
          if (c != -1) {
            for(int i = 0; i < 4; i++) {
              d = b[i] & 0xff;
              d <<= ((3 - i) * 8);
              offset += d;
              //System.out.format("%x; %x; %x|", b[i], d, offset);          
            }
            //System.out.format("%x", offset);          
          }
          
          c = inputStreamIndex.read(b, 0, 4);
          
          if (c != -1) {
            for(int i = 0; i < 4; i++) {
              d = b[i] & 0xff;
              d <<= ((3 - i) * 8);
              size += d;          
              //System.out.format("%x; %x; %x|", b[i], d, size);          
            }
            
            //System.out.format("%x", size);
          }
        }
        
        if (c != -1) {
          ind = new IndexEntry();
          ind.key    = key;
          ind.offset = offset;
          ind.size   = size;
          aIndex.put(key, ind);
          //System.out.format("%s %x %x\n", key, offset, size);
        }
        
      } while (c != -1);                
    } finally {
      if (inputStreamIndex != null) inputStreamIndex.close();
    }    
    
    return true;
  }
  
  //Получение перевода для списка слов
  private void getTranslation(String fileNameDict, String fileNameIndex) throws IOException {
    if (loadIndex(fileNameIndex)) {
      //printIndex();
      IndexEntry ind;
      StringBuilder sb = new StringBuilder();
      int c;
      
      //BufferedReader inputStreamDict = new BufferedReader(new FileReader(fileNameIndex));
      RandomAccessFile raf = new RandomAccessFile(fileNameDict, "r");
      try {
        for (String key: aWords.keySet()) {
          sb.delete(0, sb.length());
          
          //*
          //поиск слова в индексе
          ind = aIndex.get(key);
          if (ind != null) {
            raf.seek(ind.offset);
            for (int i = 0; i < ind.size; i++) {
              c = raf.read();
              if ( (c == -1) || (c == 0 ) ) break; 
              sb.append((char)c);
            }  
            aWords.put(key, sb.toString());
            //System.out.format("%s %s", ind.key, sb.toString());
          }
          //*/
        }
      } finally {
        if (raf != null) raf.close();
      }
    }      
  }
  
  public void translate(String fileNameWords, String fileNameDict, 
    String fileNameIndex) {
    
    //boolean lRes = false;
    try {
      if ( loadWords(fileNameWords) ) getTranslation(fileNameDict, fileNameIndex);
    }
    catch(IOException e) {
      System.out.println(e.getMessage());
    }             
  }
  
  public void print() {
    String value, s;
    for (String key: aWords.keySet()) {
      s = key;
      value = aWords.get(key);      
      if (value != null) s += " | " + value;
      System.out.println(s);
    }
  }

  
  public void printIndex() {
    for (String key: aIndex.keySet()) {
      System.out.println(key.toString());
    }
  }
}
